<?php

namespace app\service;

//用户抽奖服务
use app\model\LuckPool;
use app\model\User;
use app\model\UserLuck;
use app\model\UserLuckLog;
use extend\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserLuckService extends BaseService
{


    public function index()
    {


        return $this->returnSuccess();
    }


    /**
     * 抽奖池
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function pool($user_id, $type): array
    {
        $pool_list = (new LuckPool())->where('type', $type)->select();
        foreach ($pool_list as &$item) {
            $this->handleModelData($item);
        }
        return $this->returnSuccess($pool_list);
    }

    /**
     * 抽奖
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function draw($user_id, $type, $num = 1): array
    {


        //扣除玩家抽奖卷
        $user = (new User)->find($user_id);

        $user_luck = (new UserLuck())->where('user_id', $user_id)->find();
        if (!$user_luck){
            $user_luck = (new UserLuck());
            $user_luck->user_id = $user_id;
        }

        switch ($type) {
            case LOTTERY_TYPE['免费池']:
                if ($user->free_ticket < $num) {
                    return $this->returnFail('抽奖卷不足');
                }
                $user->free_ticket -= $num;
                $user_luck->free_num += $num;
                break;
            case LOTTERY_TYPE['卡牌池']:
                if ($user->card_ticket < $num) {
                    return $this->returnFail('抽奖卷不足');
                }
                $user->card_ticket -= $num;
                $user_luck->card_num += $num;
                break;
            case LOTTERY_TYPE['服装池']:
                if ($user->cos_ticket < $num) {
                    return $this->returnFail('抽奖卷不足');
                }
                $user->cos_ticket -= $num;
                $user_luck->cos_num += $num;
                break;
            case LOTTERY_TYPE['角色池']:
                if ($user->role_ticket < $num) {
                    return $this->returnFail('抽奖卷不足');
                }
                $user->role_ticket -= $num;
                $user_luck->role_num += $num;
                break;
            default:
                return $this->returnFail('抽奖类型错误');
        }
        $user->save();
        $user_luck->save();

        $pool_list = (new LuckPool())->where('type', $type)->select();

        $list = [];
        foreach ($pool_list as $item) {
            $list[$item->id] = $item->probability;
        }

        $rand = Random::lottery($list, $num,false);




        $result = [];
        if ($num == 1) {
            $result[] = $rand;
        }else{
            $result = $rand;
        }


        $user_luck_log_list = [];
        foreach ($result as $item) {
            $user_luck_log = (new UserLuckLog());
            $user_luck_log->user_id = $user_id;
            $user_luck_log->prize_id = $item;
            $user_luck_log->type = $type;
            $user_luck_log->save();
            $user_luck_log_list[] = $user_luck_log;
        }


        return $this->returnSuccess($user_luck_log_list);
    }


}